﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Musicman.Client2.Components
{
    public class RoundProgressLabel : Label
    {
        private Color OVERALL_GRADIENT_COLOR1 = Color.Orange;
        private Color OVERALL_GRADIENT_COLOR2 = Color.Red;
        private int percentage;
        private Rectangle overalRect;
        private LinearGradientBrush lgb;
        private Pen overalGradientPen;
        private Pen scalePen;

        public RoundProgressLabel()
            : base()
        {
            overalRect = new Rectangle(0, 0, this.Width, this.Height);
            lgb = new LinearGradientBrush(overalRect,
                OVERALL_GRADIENT_COLOR1, OVERALL_GRADIENT_COLOR2, 350f);
            overalGradientPen = new Pen(lgb);
            SolidBrush sb = new SolidBrush(Color.DimGray);
            scalePen = new Pen(sb);
            scalePen.Width = 15;
            this.Resize += new EventHandler(RoundProgressLabel_Resize);
        }

        private void RoundProgressLabel_Resize(object sender, EventArgs e)
        {
            overalRect = new Rectangle(10, 10, this.Width-20, this.Height-20);
            lgb = new LinearGradientBrush(overalRect,
                OVERALL_GRADIENT_COLOR1, OVERALL_GRADIENT_COLOR2, 350f);
            overalGradientPen = new Pen(lgb);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            float sweep = this.percentage == 0 ? 0 :
                (float)this.percentage / 100f * 360f;
            if (sweep != 0)
            {
                overalGradientPen.Width = 15;
                g.DrawArc(overalGradientPen, overalRect, 89, sweep + 1);
                for (int i = 0; i < 30; i++)
                {
                    g.DrawArc(scalePen, overalRect, i * 12 + 15, .5f);
                }
            }
            base.OnPaint(e);
        }

        public int Percentage
        {
            get { return percentage; }
            set
            {
                if (value != percentage)
                {
                    this.Text = value == 0 ? "" : String.Format("{0}%", percentage);
                    percentage = value;
                    Refresh();
                }
            }
        }
    }
}
